import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:xinxuan/common/dialogs/del_dialog.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:get/get.dart';
import 'package:common_utils/common_utils.dart';

class StoreOrderBtns extends StatelessWidget {

  final int status;
  /// 去支付
  final Function toPayFun;
  /// 取消订单
  final Function cleanOrderFun;
  /// 确认收货
  final Function receiveOrderFun;
  /// 物流信息
  final Function? logisticsFun;
  /// 确认收货
  final Function confirmReceiptFun;
  /// 按钮宽度
  final double btnWidth;
  /// 转提货
  final Function conversionPickingFun;
  /// 提货码
  final Function pickUpCodeFun;
  /// 查看提货码
  final Function seePickUpCodeFun;

  /// 商品类型 1 自提、2 发货
  final int pickType;

  /// 提货码
  final String? voucherCode;

  /// 确认收货按钮是否可点击
  final int isClick;

  /// 是否详情页
  final bool isDetail;
  /// 剩余可转提货时间
  final int transferRemaining;
  /// 是否为核销人员
  final int isWriteoffOfficer;
  /// 商品类型 1 普通类型 2 转入类型（转款）
  final int type;

  final int isTransfer;

  /// 核销
  final Function checkoff;

  StoreOrderBtns({
    required this.status, 
    required this.toPayFun, 
    required this.cleanOrderFun, 
    required this.receiveOrderFun,
    this.logisticsFun,
    required this.confirmReceiptFun,
    this.btnWidth = 90,
    this.isClick = 0,
    required this.conversionPickingFun,
    required this.pickUpCodeFun,
    required this.seePickUpCodeFun,
    required this.pickType,
    required this.voucherCode,
    required this.isDetail,
    required this.transferRemaining,
    required this.isWriteoffOfficer,
    required this.type,   
    required this.checkoff,
    required this.isTransfer,
  });

  final CommonPageController commonPageController = Get.find();

  @override
  Widget build(BuildContext context) {
    List<Widget> widgets = [];
    if (pickType == 1) {
      if (isWriteoffOfficer == 1 && type == 2 && status != 10) {
        widgets = [
          Container(
            width: btnWidth.w,
            margin: EdgeInsets.only(left: 12.w),
            child: ButtonUtil(
              text: '核销', 
              fontSize: 14.sp, 
              borderRadius: 18.w, 
              height: 36.h, 
              onTop: () {
                Get.dialog(DelDialog(message: '确认核销吗？', onTap: () async {
                  checkoff();
                }));
              }
            ),
          )
        ];
      } else {
        if (status == 1) {
          widgets = [
            Container(
              width: btnWidth.w,
              child: ButtonUtil(
                text: '取消订单', 
                fontSize: 14.sp, 
                borderRadius: 18.w, 
                height: 36.h, 
                textColor: textSubtitleColor,
                showBorder: true,
                borderColor: bgDisplayColor,
                bgColors: [
                  bgWhiteColor,
                  bgWhiteColor
                ],
                onTop: () {
                  Get.dialog(DelDialog(message: '确认取消订单吗？', onTap: () {
                    cleanOrderFun();
                  }));
                }
              ),
            ),
            Container(
              width: btnWidth.w,
              margin: EdgeInsets.only(left: 12.w),
              child: ButtonUtil(
                text: '去支付', 
                fontSize: 14.sp, 
                borderRadius: 18.w, 
                height: 36.h, 
                onTop: () {
                  toPayFun();
                }
              ),
            )
          ];
        } else if (status == 2 && TextUtil.isEmpty(voucherCode)) {
          widgets = [
            Container(
              width: btnWidth.w,
              margin: EdgeInsets.only(left: 12.w),
              child: Opacity(
                opacity: isTransfer == 0 ? .3 : isDetail ? transferRemaining > 0 ? .3 : 1 : 1,
                child: ButtonUtil(
                  text: '转提货', 
                  fontSize: 14.sp, 
                  borderRadius: 18.w, 
                  textColor: mainTextColor,
                  height: 36.h, 
                  showBorder: true,
                  bgColors: [
                    bgWhiteColor,
                    bgWhiteColor,
                  ],
                  onTop: () {
                    if (isTransfer == 0) {
                      return;
                    }
                    conversionPickingFun();
                  }
                )
              )
            ),
            Container(
              width: btnWidth.w,
              margin: EdgeInsets.only(left: 12.w),
              child: ButtonUtil(
                text: '提货码', 
                fontSize: 14.sp, 
                borderRadius: 18.w, 
                height: 36.h, 
                onTop: () {
                  pickUpCodeFun();
                }
              ),
            )
          ];
        } else if (status == 2 && !TextUtil.isEmpty(voucherCode) && !isDetail) {
          widgets = [
            Container(
              width: btnWidth.w,
              margin: EdgeInsets.only(left: 12.w),
              child: ButtonUtil(
                text: '查看提货码', 
                fontSize: 14.sp, 
                borderRadius: 18.w, 
                height: 36.h, 
                onTop: () {
                  seePickUpCodeFun();
                }
              ),
            )
          ];
        } else {
          widgets = [
            Expanded(child: ButtonUtil(
              text: '在线客服', 
              fontSize: 14.sp, 
              borderRadius: 18.w, 
              height: 36.h, 
              onTop: () {
                commonPageController.openWeChatCustomerServiceChat();
              }
            ))
          ];
        }
      }
    } else {
      if (status == 1) {
        widgets = [
          Container(
            width: btnWidth.w,
            child: ButtonUtil(
              text: '取消订单', 
              fontSize: 14.sp, 
              borderRadius: 18.w, 
              height: 36.h, 
              textColor: textSubtitleColor,
              showBorder: true,
              borderColor: bgDisplayColor,
              bgColors: [
                bgWhiteColor,
                bgWhiteColor
              ],
              onTop: () {
                Get.dialog(DelDialog(message: '确认取消订单吗？', onTap: () {
                  cleanOrderFun();
                }));
              }
            ),
          ),
          Container(
            width: btnWidth.w,
            margin: EdgeInsets.only(left: 12.w),
            child: ButtonUtil(
              text: '去支付', 
              fontSize: 14.sp, 
              borderRadius: 18.w, 
              height: 36.h, 
              onTop: () {
                toPayFun();
              }
            ),
          )
        ];
      } else if (status == 2 || status == 7) {
        widgets = [
          Expanded(child: ButtonUtil(
            text: '在线客服', 
            fontSize: 14.sp, 
            borderRadius: 18.w, 
            height: 36.h, 
            onTop: () {
              commonPageController.openWeChatCustomerServiceChat();
            }
          ))
        ];
      } else if (status == 5) {
        widgets = [
          Container(
            width: btnWidth.w,
            margin: EdgeInsets.only(left: 12.w),
            child: ButtonUtil(
              text: '物流信息', 
              fontSize: 14.w, 
              borderRadius: 18.w, 
              height: 36.h, 
              textColor: mainTextColor,
              showBorder: true,
              bgColors: [
                bgWhiteColor,
                bgWhiteColor,
              ],
              onTop: () => logisticsFun!()
            ),
          ),
          isClick == 0 ? Container(
            width: btnWidth.w,
            margin: EdgeInsets.only(left: 12.w),
            child: ButtonUtil(
              text: '确认收货', 
              fontSize: 14.w, 
              borderRadius: 18.w, 
              height: 36.h, 
              textColor: textSubtitleColor,
              bgColors: [
                bgDisplayColor,
                bgDisplayColor,
              ],
              onTop: () {}
            ),
          ) : Container(
            width: btnWidth.w,
            margin: EdgeInsets.only(left: 12.w),
            child: ButtonUtil(
              text: '确认收货', 
              fontSize: 14.w, 
              borderRadius: 18.w, 
              height: 36.h, 
              onTop: () => confirmReceiptFun()
            ),
          )
        ];
      } else if (status == 8) {
        widgets = [
          Container(
            width: btnWidth.w,
            margin: EdgeInsets.only(left: 12.w),
            child: ButtonUtil(
              text: '在线客服', 
              fontSize: 14.w, 
              borderRadius: 18.w, 
              height: 36.h, 
              textColor: mainTextColor,
              showBorder: true,
              bgColors: [
                bgWhiteColor,
                bgWhiteColor,
              ],
              onTop: () => commonPageController.openWeChatCustomerServiceChat()
            ),
          ),
          Container(
            width: btnWidth.w,
            margin: EdgeInsets.only(left: 12.w),
            child: ButtonUtil(
              text: '物流信息', 
              fontSize: 14.w, 
              borderRadius: 18.w, 
              height: 36.h, 
              onTop: () => logisticsFun!()
            ),
          )
        ];
      } else {
        return Container();
      }
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: widgets,
    );
  }
}